﻿using System;
using System.Linq;
using Simple.CMS.Core;
using Simple.CMS.Entities;
using Simple.Common;

namespace Simple.CMS.Web.Repository
{
    /// <summary>
    /// 数据库初始化
    /// </summary>
    public class DbInitializer
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Initaliz( CmsContext context )
        {
            context.Database.EnsureCreated();
            //
            var email = "admin@123.com";
            var hasAdmin = context.AdminUsers.Any(it=>it.Email == email);
            if (!hasAdmin)
            {
                var salt = Guid.NewGuid().ToString();
                var pwd = RsaHelper.Md5Encrypt("123", salt);
                var admin = new AdminUser
                {
                    Email = email, Salt = salt, Password = pwd, DisplayName = "管理员", Role = UserRole.SysAdmin
                };
                context.AdminUsers.Add(admin);
                context.SaveChanges();
            }
        }
    }
}
